﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
using System.Net;

namespace DotNetDream.Helper
{
    public class MailMaster
    {
        #region Fields
        //SMTP server instance
        private SmtpClient _server = new SmtpClient();
        #endregion

        #region Properties

        #endregion

        /// <summary>
        /// Instanciate an MailMaster with settings read from the config file
        /// </summary>
        public MailMaster()
        {
            _server.Host = ConfigurationManager.AppSettings["MailMasterHost"];
            _server.Port = int.Parse(ConfigurationManager.AppSettings["MailMasterPort"]);
            _server.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MailMasterUser"], ConfigurationManager.AppSettings["MailMasterPassword"]);
            _server.EnableSsl = true;
        }

        /// <summary>
        /// Send an email
        /// </summary>
        /// <param name="address"></param>
        /// <param name="content"></param>
        public void Send(string to, string subject, string body)
        {
            //Create new mail
            MailMessage mail = new MailMessage();
            mail.To.Add(to);

            mail.From = new MailAddress(ConfigurationManager.AppSettings["MailMasterEmail"], ConfigurationManager.AppSettings["MailMasterAlias"]);
            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = true;

            //Then send it
            _server.Send(mail);
        }
    }
}
